<?php
include_once '../library/config.php';
include_once '../library/httprequesthelper.php';
include_once root.'/model/shenghuodingdanlist.php';
include_once root.'/model/shenghuolist.php';
include_once root.'/model/jifenlist.php';

$postStr=file_get_contents('php://input');
$postStr=str_replace('<![CDATA[','',$postStr);
$postStr=str_replace(']]>','',$postStr);

$req=new HttpRequestHelper();
$req->keyvalue((array)simplexml_load_string($postStr));
try
{
    if($req['result_code']=='SUCCESS')
    {
        $sign=$req['sign'];
        $req->clear('sign');
        if($sign!=strtoupper($req->signusemd5(null,'&key='.weinxinprikey)))
        {
            return;
        }

        $entity=new ShengHuoDingDanList();
        if($entity->select('myid,usejifen,shuliang,shenghuoid,zongjia')->where('id=? and state=9',array($req['out_trade_no']))->get_first_rows())
        {
            $myid=$entity['myid'];
            $usejifen=$entity['usejifen'];
            $zongjia=$entity['zongjia'];
            $shenghuoid=$entity['shenghuoid'];

            $entity=new ShengHuoDingDanList();
            $entity->keyvalue('state','0');
            $entity->update()->where('id=? and state=9',array($req['out_trade_no']))->submit();

            if($usejifen)
            {
                JiFenList::jifenuse($myid,-($usejifen),$req['out_trade_no']);
            }else
            {
                JiFenList::jifenuse($myid,ceil($zongjia),$req['out_trade_no']);
            }
            ShengHuoList::kucunhuizong($shenghuoid,$req['out_trade_no']);
        }
        echo '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml> ';
    }
}catch(Exception $ex)
{
    
}
?>